<template>
    <div class="white-box">
        <div class="table-mb">
            <el-button type="primary" @click="clickAdd">添加</el-button>
            <el-button type="default" plain @click="allDel">批量删除</el-button>
        </div>

        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
            @selection-change="handleAllSelected"
        >
            <el-table-column type="selection" width="60" />
            <el-table-column prop="qrcode_id" label="ID" width="100"></el-table-column>
            <el-table-column prop="title" label="货架二维码">
                <template #default="{ row }">
                    <el-image class="table-lr__img" loading="lazy" :src="proxy.imgUrl + row.photo + '!img_400'" fit="cover" v-if="row.photo" />
                </template>
            </el-table-column>
            <el-table-column label="货架号">
                <template #default="{ row }">
                    {{ row.shelve_title + row.shelve_num }}
                </template>
            </el-table-column>
            <el-table-column prop="created_at_label" label="添加时间"></el-table-column>
            <el-table-column label="操作" width="210" class-name="operation-nav" fixed="right">
                <template #default="{ row }">
                    <el-button class="theme-btn" link type="primary" @click="clickView(row)">查看二维码</el-button>
                    <el-popconfirm title="是否确定删除？" confirm-button-text="确定" cancel-button-text="取消" @confirm="handleDelete(row)">
                        <template #reference>
                            <el-button class="theme-btn" link type="primary">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>

        <!-- 添加/编辑 -->
        <el-dialog
            v-model="dialogZd"
            title="添加货架二维码"
            width="620px"
            :append-to-body="true"
            :destroy-on-close="true"
            :close-on-click-modal="false"
            @close="closeDialog"
        >
            <el-form ref="ruleZdRef" :model="ruleZdForm" :rules="ruleCheckZd" label-width="150px">
                <el-form-item label="货架规格" prop="shelve_id">
                    <el-select
                        v-model="ruleZdForm.shelve_id"
                        clearable
                        placeholder="选择货架规格"
                        style="width: 130px"
                    >
                        <el-option :label="item.title" :value="item.shelve_id" v-for="item in shelves" :key="item.shelve_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="货架号" prop="shelve_num" required>
                    <el-input
                        v-model.number="ruleZdForm.shelve_num"
                        clearable
                        oninput="value=value.replace(/[^\d]/g,'')"
                        placeholder="请输入货架号"
                    />
                </el-form-item>
                <el-form-item>
                    <div class="flexs" style="margin-top: 20px">
                        <el-button type="primary" :loading="form_loading" @click="submitZdForm(ruleZdRef)"> 确认添加 </el-button>
                        <el-button @click="resetZdForm(ruleZdRef)">重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch, reactive } from "vue"
import OneLoad from "@mixins/one-load"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getList()
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, pubParams, pubRefreshParams, getdata } = OneLoad()
// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    pubParams(getList)
})

function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
    }
    getdata("shelve/qrcode/index", params)
}

/**
 * 添加/编辑
 * @property {Boolean} dialogZd 弹窗状态
 * @property {Boolean} form_loading 提交按钮状态
 * @property {Array} ruleZdRef 表单规则
 * @property {Object} ruleZdForm  表单字段
 * @property {Object} ruleCheckZd  表单提示
 */
const dialogZd = ref(false)
const form_loading = ref(false)
const ruleZdRef = ref(null)
const ruleZdForm = reactive({
    shelve_id: "",
    shelve_num: "",
})
const ruleCheckZd = reactive({
    shelve_id: [{ required: true, message: "请选择货架规格", trigger: "change" }],
    shelve_num: [{ required: true, message: "请输入货架号", trigger: "blur" }],
})
const submitZdForm = (formEl) => {
    if (!formEl) return
    formEl.validate((valid, fields) => {
        if (valid) {
            form_loading.value = true
            let data = {
                shelve_id: ruleZdForm.shelve_id,
                shelve_num: ruleZdForm.shelve_num,
            }
            proxy.$axios
                .post("shelve/qrcode/add", data)
                .then((res) => {
                    form_loading.value = false
                    if (res.data.code == 0) {
                        dialogZd.value = false
                        proxy.$message({ message: "保存成功！", type: "success" })
                        pubParams(getList)
                    } else {
                        proxy.$message({message:res.data.msg,type:"error"})
                    }
                })
                .catch((err) => {
                    form_loading.value = false
                })
        }
    })
}
const resetZdForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}
const closeDialog = () => {
    dialogZd.value = false
    form_loading.value = false
    resetZdForm(ruleZdRef.value)
}

// 查看二维码
const clickView = (row) => {
    window.open(`${proxy.imgUrl + row.photo}`, "_blank") // 打开浏览器新窗口展示文件
}

// 删除单个
const handleDelete = (row) => {
    postDel(row.qrcode_id)
}
// 删除多个
const ids = ref([])
const handleAllSelected = (val) => {
    let arrs = []
    for (let i = 0; i < val.length; i++) {
        arrs.push(val[i].qrcode_id)
    }
    ids.value = arrs
}
const allDel = () => {
    if (ids.value.length == 0) {
        proxy.$message({ message: "请选择需要删除的数据", type: "warning" })
        return
    }
    postDel(ids.value)
}
// 删除接口
function postDel(params) {
    proxy.$axios
        .post("shelve/qrcode/delete", {
            qrcode_id: params,
        })
        .then((res) => {
            if (res.data.code == 0) {
                proxy.$message({ message: '删除成功！', type: "success" })
                pubParams(getList)
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}

const clickAdd = () => {
    dialogZd.value = true
    getShelves()
}
// 获取货架规格
const shelves = ref([])
function getShelves() {
    proxy.$axios
        .get("shelve/qrcode/add")
        .then((res) => {
            if (res.data.code == 0) {
                shelves.value = res.data.data
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}
</script>
<script>
export default {
    name: "ShelveCode",
}
</script>

<style lang="less" scoped>
.table-lr__img {
    width: 80px;
    height: 80px;
    overflow: hidden;
}
</style>
